package com.scqkxx.gamebox.activity.news;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.news.adapter.NewsMarksAdapter;
import com.scqkxx.gamebox.databinding.ActivityNewsMarksBinding;
import com.scqkzqtz.base.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class NewsMarksActivity extends BaseActivity<ActivityNewsMarksBinding> {

    private List<AVObject> dataList = new ArrayList<>();
    private NewsMarksAdapter newsMarksAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_news_marks;
    }

    @Override
    public void init() {
        binding.titleView.setOnTitleClickListener((int titleType) -> finish());

        newsMarksAdapter = new NewsMarksAdapter(R.layout.item_news_marks,dataList);
        binding.rvMarkList.setLayoutManager(new LinearLayoutManager(this));
        binding.rvMarkList.setAdapter(newsMarksAdapter);

        int goodsId = getIntent().getIntExtra("goodsId",0);
        getGoods(goodsId);
        getMarks(goodsId);
    }

    private void getGoods(int goodsId){
        AVQuery<AVObject> query = AVQuery.getQuery("Goods");
        query.whereEqualTo("objectId",goodsId);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                Log.i("Goods",avObjects+"");
                if(avException == null && avObjects != null && avObjects.size() > 0){
                    AVObject avObject = avObjects.get(0);
                    binding.tvMarkName.setText(avObject.getString("name"));
                }
            }
        });
    }

    private void getMarks(int goodsId){
        AVQuery<AVObject> query = AVQuery.getQuery("Marks");
        query.whereEqualTo("goodsId",goodsId);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                Log.i("Marks",avObjects+"");
                if(avException == null){
                    dataList.clear();
                    dataList.addAll(avObjects);
                    newsMarksAdapter.notifyDataSetChanged();

                    binding.tvMarkCount.setText("共"+dataList.size()+"人标记");
                }
            }
        });
    }

}
